from flask import Flask, request
from codeforces.tables import mnnu_group_id, mnnu_users_name
from codeforces.methods import sendGroupRatingRank, sendPrivateRatingRank, sendGroupAcRank, \
    sendPrivateAcRank, sendPrivateContests, sendGroupContests,sendGroupHelp,sendPrivateHelp,sendPrivateUser,sendGroupUser,\
    sendGroupAtcoderContests,sendGroupNowCoderContests,sendPrivateNowCoderContests,sendPrivateAtcoderContests

app = Flask(__name__)

@app.route('/', methods=["POST"])
def post_data():
    if request.get_json().get("message_type") == "private":  # 如果是私聊信息
        uid = request.get_json().get('sender').get('user_id')  # 获取信息发送者的 QQ号码
        nickname = request.get_json().get("sender").get("nickname")
        message = request.get_json().get('raw_message')  # 获取原始信息
        if message == "atcoder -c" or message == "atcoder contests":
            print(uid, nickname, message)
            sendPrivateAtcoderContests(uid,nickname)
        if message == "nowcoder -c" or message == "nowcoder contests":
            print(uid, nickname, message)
            sendPrivateNowCoderContests(uid, nickname)
        if message == "cf -a" or message == "cf acRank":
            print(uid, nickname, message)
            sendPrivateAcRank(uid, mnnu_users_name)
        if message == "cf -c" or message == "cf contests":
            print(uid, nickname, message)
            sendPrivateContests(uid)
        if message == "cf -h" or message == "cf help" :
            print(uid, nickname, message)
            sendPrivateHelp(uid)
        if message == "cf -r" or message == "cf rank":
            print(uid, nickname, message)
            sendPrivateRatingRank(uid, mnnu_users_name)
        if message.startswith("cf -u") or message.startswith("cf user"):
            print(uid, nickname, message)
            userName = ""
            if message.startswith("cf -u"):
                userName = message.split("cf -u", 1)[-1].strip()
            elif  message.startswith("cf user"):
                userName = message.split("cf user", 1)[-1].strip()
            sendPrivateUser(uid,userName)

    if request.get_json().get('message_type') == 'group':  # 如果是群聊信息
        gid = request.get_json().get('group_id')  # 获取群号
        uid = request.get_json().get('sender').get('user_id')  # 获取信息发送者的 QQ号码
        nickname = request.get_json().get("sender").get("nickname")
        message = request.get_json().get('raw_message')  # 获取原始信息
        if message == "atcoder -c" or message == "atcoder contests":
            print(uid, nickname, message)
            sendGroupAtcoderContests(gid,uid,nickname)
        if message == "nowcoder -c" or message == "nowcoder contests":
            print(uid, nickname, message)
            sendGroupNowCoderContests(gid,uid,nickname)
        if message == "cf -a" or message == "cf acRank":
            print(uid, nickname, message)
            sendGroupAcRank(gid, mnnu_users_name, uid)
        if message == "cf -c" or message == "cf contests":
            print(uid, nickname, message)
            sendGroupContests(gid, nickname,uid)
        if message == "cf -h" or message == "cf help" :
            print(uid, nickname, message)
            sendGroupHelp(gid,uid)
        if message == "cf -r" or message == "cf rank":
            print(uid, nickname, message)
            sendGroupRatingRank(gid, mnnu_users_name, uid)
        if message.startswith("cf -u") or message.startswith("cf user"):
            print(uid, nickname, message)
            userName = ""
            if message.startswith("cf -u"):
                userName = message.split("cf -u", 1)[-1].strip()
            elif  message.startswith("cf user"):
                userName = message.split("cf user", 1)[-1].strip()
            sendGroupUser(gid,uid,userName)
    return "OK"

if __name__ == '__main__':
    target=app.run(debug=True, host='127.0.0.1', port=5705)

